﻿using Sumc.Client.ModernUI.Common;
using Sumc.Models.Responses;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sumc.Client.ModernUI.Services
{
    public sealed class ScheduleRepository : BaseRepository, IScheduleRepository
    {
        public async Task<ScheduleDirections> GetTransportDirections(
            TransportType transportType, string transportNumber)
        {
            this.expireTime = new TimeSpan(1, 0, 0, 0);
            var url = string.Format("{0}Schedules?transportType={1}&transportNumber={2}",
                Constants.BaseSumcApiUrl, transportType, transportNumber);
            var directions = await this.Get<ScheduleDirections>(url);
            return directions;
        }

        public async Task<Schedule> GetTransportSchedule(
            TransportType transportType, string lid, string rid, string stopValue)
        {
            this.expireTime = new TimeSpan(1, 0, 0);
            var url = string.Format("{0}Schedules?transportType={1}&stopValue={2}&lid={3}&rid={4}",
                Constants.BaseSumcApiUrl, transportType, stopValue, lid, rid);
            var schedule = await this.Get<Schedule>(url);
            return schedule;
        }

        public async Task<Schedule> GetTransportSchedule(string parameters)
        {
            this.expireTime = new TimeSpan(1, 0, 0);
            var encodedParameters = parameters.Replace("&", "%26");
            var url = string.Format("{0}Schedules/Url?urlParameters={1}",
                Constants.BaseSumcApiUrl, encodedParameters);
            var schedule = await this.Get<Schedule>(url);
            return schedule;
        }

        public async Task<ScheduleVirtualTable> GetTransportVirtualTable(string parameters)
        {
            this.expireTime = new TimeSpan(0, 3, 0);
            var url = string.Format("{0}Schedules/VirtualTable?urlParameters={1}",
                Constants.BaseSumcApiUrl, parameters);
            var schedule = await this.Get<ScheduleVirtualTable>(url);
            return schedule;
        }


        public async Task<IEnumerable<string>> GetTransportSchedule(
            TransportType transportType, string lid, string rid, string stopValue, DateTime dateTime)
        {
            this.expireTime = new TimeSpan(1, 0, 0, 0);
            var url = string.Format("{0}TransportNextArriveTimes?transportType={1}&stopValue={2}&lid={3}&rid={4}&from={5}",
                Constants.BaseSumcApiUrl, transportType, stopValue, lid, rid, dateTime);
            var schedule = await this.Get<IEnumerable<string>>(url);
            return schedule;
        }

        public async Task<IEnumerable<string>> GetTransportSchedule(string parameters, DateTime dateTime)
        {
            this.expireTime = new TimeSpan(1, 0, 0, 0);
            var url = string.Format("{0}Schedules/UrlNextArriveTimes?urlParameters={1}&from={2}",
                Constants.BaseSumcApiUrl, parameters, dateTime);
            var schedule = await this.Get<IEnumerable<string>>(url);
            return schedule;
        }
    }
}